<template>
  <view
    v-if="shenheStatus == 0"
    class="diy-my-page"
    @touchstart="pageTouchStart"
    @touchmove="pageTouchMove"
    @touchend="pageTouchEnd"
  >
    <diyGoodsDetail
      v-if="goods_id && goods_id > 0"
      :goods_id="goods_id"
      :mdId="mdId"
      :mdData="mdData"
      :pageScrollTop="pageScrollTop"
      :hideTopTab="hideTopTab"
      :topTabActiveIndex="topTabActiveIndex"
      @goodsBlockLocalData="setGoodsBlockLocal"
      @goodsEvaluateLocalData="setGoodsEvaluateLocal"
      @goodsRichTextLocalData="setGoodsRichTextLocal"
    ></diyGoodsDetail>
    <!-- 全局过渡组件 -->
    <transitionPage></transitionPage>
  </view>
  <Shenhe v-else-if="shenheStatus == 1" :isShowTitle="true"></Shenhe>
</template>

<script>
import Shenhe from "@/components/shenhe/shenhe";
import diyGoodsDetail from "./components/diySystemPage/goodsDetail/index.vue";

export default {
  components: {
    diyGoodsDetail,
    Shenhe
  },
  data() {
    return {
      goods_id: "",
      routeData: null,
      mdId: "0",
      mdData: {},
      pageScrollTop: 0,
      scrollStart: 0,
      scrollWay: "",
      hideTopTab: true,
      goodsBlockLocal: {
        t: 0,
        h: 0
      }, // 商品内容区域
      goodsEvaluateLocal: {
        t: 0,
        h: 0
      }, // 商品评价区域
      goodsRichTextLocal: {
        t: 0,
        h: 0
      }, // 商品详情区域
      topTabActiveIndex: 0
    };
  },
  computed: {
    screenWidth() {
      const res = uni.getSystemInfoSync();
      return res.screenWidth;
    }
  },
  onLoad(t) {
    if (t?.id) {
      this.goods_id = t.id;
      this.routeData = t;
      let mdLocalData = uni.getStorageSync("storeMdData");
      if (mdLocalData) {
        let mdData = JSON.parse(mdLocalData);
        if (mdData?.id) {
          this.mdId = mdData.id;
          this.getLocalAddress(this.mdId);
        }
      }
    }
    if (t?.md_id) {
      this.mdId = t.md_id;
      this.getLocalAddress(this.mdId, "saveMd");
      uni.setStorageSync("MdDataReload", "true");
    }
    if (t?.scene) {
      let scene = decodeURIComponent(t.scene);
      let params = this.$utils.sceneDecode(scene);
      this.routeData = params;
      this.routeData.id = params.gid;
      if (params.gid) {
        this.goods_id = params.gid;
      }
      if (params.md_id && params.md_id > 0) {
        this.mdId = params.md_id;
        this.getLocalAddress(this.mdId, "saveMd");
        uni.setStorageSync("MdDataReload", "true");
      }
    }
  },
  onPageScroll(e) {
    this.pageScrollTop = e.scrollTop;
    if (e.scrollTop >= this.screenWidth / 2) {
      this.hideTopTab = false;
    } else {
      this.hideTopTab = true;
    }

    if (
      e.scrollTop >= this.goodsBlockLocal.t &&
      e.scrollTop <= this.goodsBlockLocal.h
    ) {
      this.topTabActiveIndex = 0;
    }
    if (
      e.scrollTop >= this.goodsEvaluateLocal.t &&
      e.scrollTop <= this.goodsEvaluateLocal.h
    ) {
      this.topTabActiveIndex = 1;
    }
    if (
      e.scrollTop >= this.goodsRichTextLocal.t &&
      e.scrollTop <= this.goodsRichTextLocal.h
    ) {
      this.topTabActiveIndex = 2;
    }
  },
  onShow() {
    let isUsePreviewImg = uni.getStorageSync("usePreviewImgApi");
    if (isUsePreviewImg == "true") {
      uni.removeStorageSync("usePreviewImgApi");
      return;
    }

    // 重新渲染组件
    // this.goods_id = '-1';
    // setTimeout(() => {
    //   if (this.routeData) this.goods_id = this.routeData.id;
    // }, 0);

    // 判断是否登录
    const isLogin = this.$utils.isLogin(false);
    if (!isLogin) {
      this.$store.commit(
        "setNoLoginTabarPath",
        `/goods/goods/goods?id=${this.routeData.id}`
      );
      this.$store.commit("setRouteType", "redirectTo");
    } else {
      this.$store.commit("setNoLoginTabarPath", "");
      this.$store.commit("setRouteType", "");
    }
  },
  watch: {},
  methods: {
    async getLocalAddress(id, type) {
      const res = await this.$store.dispatch("getLocation");
      if (res) {
        let { longitude, latitude } = res;

        this.getMdData({
          longitude,
          latitude,
          md_id: id,
          type
        });
      }
    },
    setGoodsBlockLocal(e) {
      if (e?.h) {
        this.goodsBlockLocal.t = e.t;
        this.goodsBlockLocal.h = e.t + e.h;
      }
    },
    setGoodsEvaluateLocal(e) {
      if (e?.h) {
        this.goodsEvaluateLocal.t = e.t;
        this.goodsEvaluateLocal.h = e.t + e.h;
      }
    },
    setGoodsRichTextLocal(e) {
      if (e?.h) {
        this.goodsRichTextLocal.t = e.t;
        this.goodsRichTextLocal.h = e.t + e.h;
      }
    },
    pageTouchStart(e) {
      this.scrollStart = e.touches[0].pageY;
    },
    pageTouchMove(e) {
      let y = e.touches[0].pageY;
      if (y - this.scrollStart > 0) {
        if (this.hideTopTab) return;
        this.hideTopTab = true;
      }
    },
    pageTouchEnd(e) {
    },
    getMdData(params) {
      this.$allrequest.goods.getMdAddress(params).then((res) => {
        if (!res.code && res.data) {
          this.mdData = res.data;
          if (params.type == "saveMd") {
            uni.setStorageSync("storeMdData", JSON.stringify(res.data));
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.diy-my-page {
  width: 100%;
}
</style>
